package cn.wolfcode.service.impl;

import cn.wolfcode.entity.Destination;
import cn.wolfcode.entity.Region;
import cn.wolfcode.mapper.DestinationMapper;
import cn.wolfcode.query.DestinationQuery;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IRegionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author c1eye
 * @since 2022-11-03
 */
@Service
public class DestinationServiceImpl extends ServiceImpl<DestinationMapper, Destination> implements IDestinationService {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private DestinationMapper destinationMapper;

    @Override
    public List<Destination> getDestByRegionId(int rid) {
        Region region = regionService.getById(rid);
        String[] refs = region.getRefIds().split(",");
        ArrayList<Long> ids = new ArrayList<>();
        for (String ref : refs) {
            ids.add(Long.parseLong(ref));
        }
        return destinationMapper.selectBatchIds(ids);
    }

    @Override
    public Page<Destination> queryPage(DestinationQuery destinationQuery) {

        Page<Destination> page = new Page<>();
        page.setCurrent(destinationQuery.getCurrentPage());
        page.setSize(destinationQuery.getPageSize());

        QueryWrapper<Destination> wrapper = new QueryWrapper<>();
        wrapper.isNull(destinationQuery.getParentId() == null, "parent_id");
        wrapper.eq(destinationQuery.getParentId() != null, "parent_id", destinationQuery.getParentId());
        return destinationMapper.selectPage(page, wrapper);
    }

    @Override
    public List<Destination> queryToasts(Long parentId) {
        ArrayList<Destination> list = new ArrayList<>();
        createToasts(list, parentId);
        Collections.reverse(list);
        return list;
    }

    @Override
    public List<Destination> search(int rid) {
        QueryWrapper<Destination> queryWrapper = new QueryWrapper<>();
        List<Destination> list;
        if (rid == -1) {
            queryWrapper.eq("parent_id", 1);
            list = destinationMapper.selectList(queryWrapper);
        } else {
            list = getDestByRegionId(rid);
        }

        for (Destination destination : list) {
            QueryWrapper<Destination> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id", destination.getId()).last("limit 10");
            destination.setChildren(destinationMapper.selectList(wrapper));
        }
        return list;
    }

    @Override
    public Destination queryByName(String keyword) {
        QueryWrapper<Destination> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", keyword);
        return destinationMapper.selectOne(queryWrapper);
    }

    private void createToasts(List<Destination> list, Long destId) {
        if (destId == null) {
            return;
        }
        Destination destination = getById(destId);
        list.add(destination);
        createToasts(list, destination.getParentId());
    }
}
